package com.webserve.core;

import com.webserve.http.HttpServletRequest;
import com.webserve.http.HttpServletResponse;
import com.webserve.usercontrol.UserController;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;

public class DispatcherServlet {
    private static DispatcherServlet servlet = new DispatcherServlet();
    private static File rootDir;
    private static File staticDir;

    static {
        try {
            rootDir = new File(
                    DispatcherServlet.class.getClassLoader().
                            getResource(".").toURI());
            staticDir = new File(rootDir, "static");
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }


    }

    private DispatcherServlet() {}

    public static DispatcherServlet getServlet() {
        return servlet;
    }

    public void start(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String path = request.getRequestURI();
        System.out.println(path);
        if ("/regUser".equals(path)) {
            UserController controller = new UserController();
            controller.reg(request, response);
        } else {
            File file = new File(staticDir, path);
            if (file.isFile()) {
            } else {
                file = new File(staticDir, "root/notfound.html");
                response.setStatusCode(404);
                response.setStatusReason("NotFound");
            }
            response.setContentFile(file);
        }


    }

    public void reg() {


    }


}
